package com.automannn.atm_back.modules.codeGenerator.service.impl;

import com.automannn.atm_back.modules.codeGenerator.entity.ColumnConfigEntity;
import com.automannn.atm_back.modules.codeGenerator.entity.ColumnConfigGroupEntity;
import com.automannn.atm_back.modules.codeGenerator.entity.ColumnEntity;
import com.automannn.atm_back.modules.codeGenerator.entity.TableEntity;
import com.automannn.atm_back.modules.codeGenerator.handler.FormItemRuleHandler;
import com.automannn.atm_back.modules.codeGenerator.handler.MapperHandler;
import com.automannn.atm_back.modules.codeGenerator.mapper.ColumnConfigEntityMapper;
import com.automannn.atm_back.modules.codeGenerator.mapper.ColumnConfigGroupEntityMapper;
import com.automannn.atm_back.modules.codeGenerator.service.ColumnConfigGroupEntityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
* (GeneratorColumnConfigGroup)表服务实现类
*
* @author chenkaihai
* @since 2020-07-12 16:52:30
*/
@Service
public class ColumnConfigGroupEntityServiceImpl implements ColumnConfigGroupEntityService {
    @Resource
    private ColumnConfigGroupEntityMapper columnConfigGroupEntityMapper;

    @Resource
    private ColumnConfigEntityMapper columnConfigEntityMapper;

    @Resource
    private MapperHandler mapperHandler;

    @Resource
    private FormItemRuleHandler formItemRuleHandler;


    /**
    * 查询一个对象
    *
    * @param  id
    * return 实例对象
    */
    @Override
    public ColumnConfigGroupEntity queryOne(String id) {
        return this.columnConfigGroupEntityMapper.queryById(id);
    }

    /**
    * 查询所有对象
    *
    * return 实例对象
    */
    @Override
    public IPage<ColumnConfigGroupEntity> queryAll(Page<ColumnConfigGroupEntity> page, ColumnConfigGroupEntity generatorColumnConfigGroup) {
        return this.columnConfigGroupEntityMapper.queryAll(page,generatorColumnConfigGroup);
    }

    /**
    * 新增数据
    *
    * @param generatorColumnConfigGroup 实例对象
    * @return 实例对象
    */
    @Override
    public ColumnConfigGroupEntity insert(ColumnConfigGroupEntity generatorColumnConfigGroup) {
        this.columnConfigGroupEntityMapper.insert(generatorColumnConfigGroup);
        return generatorColumnConfigGroup;
    }

    /**
    * 修改数据
    *
    * @param generatorColumnConfigGroup 实例对象
    * @return 实例对象
    */
    @Override
    public void update(ColumnConfigGroupEntity generatorColumnConfigGroup) {
         columnConfigGroupEntityMapper.update(generatorColumnConfigGroup);
    }

    /**
    * 通过主键删除数据
    *
    * @param id 主键
    * @return 是否成功
    */
    @Override
    public boolean deleteById(String id) {
        return this.columnConfigGroupEntityMapper.deleteById(id) > 0;
    }

    @Override
    public void deleteBatch(String ids) {
        this.columnConfigGroupEntityMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
    }

    @Override
    @Transactional
    public void buildColumnConfigBySource(TableEntity tableEntity, List<ColumnEntity> itemList, List typeMapperList) {
        ColumnConfigGroupEntity columnConfigGroupEntity = new ColumnConfigGroupEntity();
        columnConfigGroupEntity.setName(tableEntity.getName());
        columnConfigGroupEntity.setNote(tableEntity.getComment());
        columnConfigGroupEntity.setSourceName(tableEntity.getName());
        ColumnConfigGroupEntity result= this.insert(columnConfigGroupEntity);
        itemList.forEach(item->{
            ColumnConfigEntity columnConfigEntity = item.toColumnConfigEntity();
            columnConfigEntity.setColumnConfigGroupId(result.getId());
            //处理映射
            mapperHandler.handle(columnConfigEntity,typeMapperList);
            formItemRuleHandler.handle(columnConfigEntity);
            columnConfigEntityMapper.insert(columnConfigEntity);
        });
    }
}
